<?php

class FederalMemberCsvHydrator extends AbstractRowCsvHydrator
{
  
  protected $member;
  protected $year;

  public function __construct (Member $member, $year)
  {
    $this->member = $member;
    $this->year = $year;
  }

  function hydrate ()
  {
    return self::arrayToCsv(array(
      $this->getSalutation(),                 //'Anrede',
      $this->getFirstName(), //'Vorname',
      $this->getLastName(), //'Name',
      $this->getStreet(), //'Straße',
      $this->getZipCode(), //'PLZ',
      $this->getCity(), //'Ort',
      $this->getCountry(), //'Land',
      $this->getPhoneNumber(), //'Telefon',
      $this->getMobilePhoneNumber(), //'Handynummer',
      $this->getEmail(), //'Email',
      $this->getDateOfBirth(), //'Geburtsdatum',
      $this->getDateOfJoining(), //'Eintrittsdatum',
      $this->getAgeGroup(), //'Stufe',
      $this->getGroup(), //'Gruppe',
      $this->getMembershipCard(), //'Mitglieds- ausweis',
      $this->getWoggle(), //'Knotenschild',
      $this->getMembershipFee(), //'Beitrag 2012',
      $this->getDateOfMembershipFeePayment(), //'Zahlungs- eingang',
      $this->getChapter(), //'Stamm',
      $this->getStateAssociation(), //'Landesverband'
    ));
  }
  
  public function getSalutation() {
    return ($this->member->getGender() == 'female' ? 'Frau' : 'Herr');
  }
  
  public function getFirstName() {
    return $this->member->getFirstName();
  }
  
  public function getLastName() {
    return $this->member->getLastName();
  }
  
  public function getStreet() {
    $address = $this->member->getFirstAddress();
    return sprintf('%s %s', $address->getStreet(), $address->getStreetNumber());
  }
  
  public function getZipCode() {
    return $this->member->getFirstAddress()->getZipCode();
  }
  
  public function getCity() {
    return $this->member->getFirstAddress()->getCity();
  }
  
  public function getCountry() {
    return $this->member->getFirstAddress()->getCountry();
  }
  
  public function getPhoneNumber() {
    $phone_numbers = $this->member->getPhoneNumbers();
    foreach($phone_numbers as $phone_number) {
      if($phone_number->getType() == '' || $phone_number->getType() == 'landline') {
        return $phone_number->getAreaCode().$phone_number->getCallThrough();
      }
    }
    return '';
  }
  
  public function getMobilePhoneNumber() {
    $phone_numbers = $this->member->getPhoneNumbers();
    foreach($phone_numbers as $phone_number) {
      if($phone_number->getType() == 'mobile') {
        return $phone_number->getAreaCode().$phone_number->getCallThrough();
      }
    }
    return '';
  }
  
  public function getEmail() {
    return $this->member->getEmailAddress();
  }
  
  public function getDateOfBirth() {
    return $this->member->getDateOfBirth()->format('d.m.Y');
  }
  
  public function getDateOfJoining() {
    return $this->member->getDateOfJoining()->format('d.m.Y');
  }
  
  public function getAgeGroup() {
    return $this->member->getAgeGroup();
  }
  
  public function getGroup() {
    $unit = $this->member->getOrganizationalUnit();
    if($unit->getLevel() == 3) {
      return $unit->getName();
    } else {
      return 'Gruppenlos';
    }
  }
  
  public function getMembershipCard() {
    return '';
  }
  
  public function getWoggle() {
    switch($this->member->getWoggleId()) {
      case 1:
        return 'blaues Knotenschild';
        break;
      case 2:
        return 'rotes Knotenschild';
        break;
      case 3:
        return 'grünes Knotenschild';
        break;
      case 4:
        return 'weißes Knotenschild';
        break;
      case 5:
        return 'schwarzes Knotenschild';
        break;
      case 6:
        return 'goldenes Knotenschild';
        break;
      default:
        return 'kein';
        break;
    }
  }
  
  public function getMembershipFee() {
    if($this->member->hasPayedMembershipFee($this->year)) {
      $payment = $this->member->getMembershipFeePaymentForYear($this->year);
      $fee = $payment->getAmount();
      if(is_numeric($fee)) {
        return str_replace('.', ',', $fee);
      }
    }
    return '';
  }
  
  public function getDateOfMembershipFeePayment() {
    if($this->member->hasPayedMembershipFee($this->year)) {
      $payment = $this->member->getMembershipFeePaymentForYear($this->year);
      $date = $payment->getDateOfPayment();
      if($date instanceof DateTime) {
        return $date->format('d.m.Y');
      }
      //$this->member->getMembershipFeePaymentForYear($this->year)->getDateOfPayment()->format('d.m.Y'); // TODO
    }
    return '';
  }
  
  public function getChapter() {
    $unit = $this->member->getOrganizationalUnit();
    if($unit->getLevel() == 2) {
      return $unit->getName();
    } else {
      return $unit->getParent()->getName();
    }
  }
  
  public function getStateAssociation() {
    return 'Baden-Württemberg';
  }

}